extends NodeState

@export var igorBoss: IgorBoss

var velocity: Vector2


func _on_process(delta : float) -> void:
	if _changing_state:
		return
	igorBoss.position += velocity * delta
	process_decelerate(delta)
	process_animation()


func _on_enter() -> void:
	velocity = Vector2.ZERO
	process_knockback()
	igorBoss.look_at_player()


func _on_exit() -> void:
	pass


func process_animation():
	igorBoss.animationPlayer.play(IgorBoss.ANIMATION_DEFENSE)


func process_knockback():
	var emit_direction := igorBoss.take_damage_emit_direction()
	if emit_direction == null:
		return
	var direction: Vector2 = Vector2.RIGHT if emit_direction.x > 0 else Vector2.LEFT
	velocity = direction * igorBoss.defense_knockback_intensity


func process_decelerate(delta : float):
	# 速度接近为0时，切换状态
	if velocity.x < 0.0001:
		transition_state(IgorBoss.STATE_IDLE)
		return
	velocity = velocity.move_toward(Vector2.ZERO, delta * igorBoss.defense_knockback_deceleration)
